package com.example.materials_management.controller;

import com.alibaba.fastjson.JSON;
import com.example.materials_management.entity.MaterialsBasicInfoEntity;
import com.example.materials_management.service.MaterialsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/materials")
public class MaterialsInfoController {
    @Autowired
    MaterialsInfoService materialsInfoService;

    @GetMapping("/all")
    public List<Map<String, Object>> getAll() {
        return materialsInfoService.getItems(null, null, null, null, null, null, null);
    }

    @GetMapping("/query")
    public List<Map<String, Object>> get(
            @RequestParam(required = false) Integer id,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String specification,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String measureUnit,
            @RequestParam(required = false) Integer remainingAmountMin,
            @RequestParam(required = false) Integer remainingAmountMax

    ) {
        return materialsInfoService.getItems(id, name, specification, category, measureUnit, remainingAmountMin, remainingAmountMax);
    }

    @PostMapping("/add")
    public Map<String, Object> addItems(@RequestBody List<MaterialsBasicInfoEntity> entities) {
        return materialsInfoService.addItems(entities);
    }

    @PostMapping("/modify")
    public Map<String, Object> modifyItem(@RequestBody Map<String, Object> requestBody) {
        return materialsInfoService.modifyItem(
                (Integer) requestBody.get("id"),
                JSON.parseObject(JSON.toJSONString(requestBody.get("entity")), MaterialsBasicInfoEntity.class)
        );
    }
}
